package com.example.demo.Controller;

import com.example.demo.Dao.PersonRepository;
import com.example.demo.Entity.Person;
import com.example.demo.Qo.PersonQo;
import com.example.demo.Utils.CodeUtils;
import com.example.demo.Utils.ConstUtils;
import com.example.demo.Utils.CosUtils;
import com.example.demo.Utils.JavaWebTokenUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * ComonApiController
 *
 * @blame Android Team
 */
@RestController
@RequestMapping("/api")
@CrossOrigin
public class CommonApiController {
    @Resource
    private PersonRepository personRepository;
    @PostMapping("/avatar")
    public CodeUtils.Message avatarUpload(@RequestParam("file") MultipartFile[]file , HttpServletRequest request ) {
        if (file == null) {
            return CodeUtils.Message.builder().code(44033).message("上传文件为空").build();
        }
        String token  = request.getHeader(ConstUtils.TokenUtils.TOKEN_HEADERS).replace(ConstUtils.TokenUtils.TOKEN_PREFIX,"");
        String userName = JavaWebTokenUtils.getUsernameFromToken(token);
        String result = CosUtils.setFile(CosUtils.AVATAR_NAME,userName,file);
        Person p = personRepository.findByUsername(userName);
        p.setAvatar(result);
        personRepository.save(p);
        if(result!=null) {
            return CodeUtils.Message.builder().code(20000).message(result).build();
        }
        return CodeUtils.Message.builder().code(55044).message("上传失败").build();
    }
    @Transactional
    @GetMapping("/fixrealname")
    public CodeUtils.Message fixRealName(@RequestParam String realName,HttpServletRequest request)
    {
        String token =  request.getHeader(ConstUtils.TokenUtils.TOKEN_HEADERS).replace(ConstUtils.TokenUtils.TOKEN_PREFIX,"");
        String userName = JavaWebTokenUtils.getUsernameFromToken(token);
        if(personRepository.updateRealName(realName,userName,new Date())!=0)
        {
            return CodeUtils.Message.builder().message("修改成功").code(20000).build();
        }
        return CodeUtils.Message.builder().message("修改成功").code(4).build();
    }
    @Transactional
    @GetMapping("/fixarea")
    public CodeUtils.Message fixArea(@RequestParam String area,HttpServletRequest request)
    {
        String token =  request.getHeader(ConstUtils.TokenUtils.TOKEN_HEADERS).replace(ConstUtils.TokenUtils.TOKEN_PREFIX,"");
        String userName = JavaWebTokenUtils.getUsernameFromToken(token);
        if(personRepository.updateArea(area,userName,new Date())!=0)
        {
            return CodeUtils.Message.builder().message("修改成功").code(20000).build();
        }
        return CodeUtils.Message.builder().message("修改失败").code(44033).build();
    }
    @GetMapping("/info")
    public CodeUtils.Message getPersonQo(HttpServletRequest request)
    {

        String token = request.getHeader(ConstUtils.TokenUtils.TOKEN_HEADERS);
        if (token==null)
        {
            return CodeUtils.Message.builder().message("获取token失败").code(44033).build();
        }
        token = token.replace(ConstUtils.TokenUtils.TOKEN_PREFIX,"");
        String name = JavaWebTokenUtils.getUsernameFromToken(token);
        if (name==null)
        {
            return CodeUtils.Message.builder().message("token有误，解析失败").code(44033).build();
        }
        Person person = personRepository.findByUsername(name);

        try {
            if (person==null)
            {
                return   CodeUtils.Message.builder().message(new ObjectMapper().writeValueAsString("没有找到该用户信息")).code(44033).build();
            }
            PersonQo personQo = PersonQo.builder()
                    .id(person.getId())
                    .role(person.getRole())
                    .realName(person.getRealName())
                    .openId(person.getOpenId())
                    .isHealth(person.getIsHealth())
                    .avatar(person.getAvatar())
                    .area(person.getArea())
                    .username(person.getUsername())
                    .updatedTime(person.getUpdatedTime())
                    .createdTime(person.getCreatedTime()).build();
            return   CodeUtils.Message.builder().message(new ObjectMapper().writeValueAsString(personQo)).code(20000).build();
        }catch (Exception e){
            e.printStackTrace();
            return CodeUtils.Message.builder().message("获取信息失败").code(55044).build();
        }
    }
    @Transactional
    @GetMapping("cancelOpenId")
    public CodeUtils.Message cancelOpenId(HttpServletRequest httpServletRequest){
        String token = httpServletRequest.getHeader(ConstUtils.TokenUtils.TOKEN_HEADERS);
        if (token==null)
        {
            return CodeUtils.Message.builder().message("获取token失败").code(44033).build();
        }
        token = token.replace(ConstUtils.TokenUtils.TOKEN_PREFIX,"");
        String name = JavaWebTokenUtils.getUsernameFromToken(token);
        if (name==null)
        {
            return CodeUtils.Message.builder().message("token有误，解析失败").code(44033).build();
        }
        return   CodeUtils.Message.builder().message(String.valueOf(personRepository.cancelAssociate(name))).code(20000).build();
    }
}
